#include "sdf.h"
#include <stdlib.h>
#include <string.h>

// 模拟设备句柄和密钥句柄
void *g_DeviceHandle = NULL;
void *g_KeyHandle = NULL;

int SDF_OpenDevice(void **phDeviceHandle) {
    g_DeviceHandle = malloc(sizeof(void*)); // 分配设备句柄
    *phDeviceHandle = g_DeviceHandle;
    return SDR_OK;
}

int SDF_CloseDevice(void *hDeviceHandle) {
    free(hDeviceHandle);
    g_DeviceHandle = NULL;
    return SDR_OK;
}

int SDF_GetDeviceInfo(void *hSessionHandle, DEVICEINFO *pstDeviceInfo) {
    // 模拟获取设备信息
    strcpy((char *)pstDeviceInfo->IssuerName, "Mock Device Manufacturer");
    strcpy((char *)pstDeviceInfo->DeviceName, "Mock Device");
    strcpy((char *)pstDeviceInfo->DeviceSerial, "1234567890123456");
    pstDeviceInfo->DeviceVersion = 1;
    pstDeviceInfo->StandardVersion = 1;
    pstDeviceInfo->AsymAlgAbility[0] = 0x01; // 支持ECC
    pstDeviceInfo->AsymAlgAbility[1] = 0x00;
    pstDeviceInfo->SymAlgAbility = 0x01; // 支持SM4
    pstDeviceInfo->HashAlgAbility = 0x01; // 支持SM3
    pstDeviceInfo->BufferSize = 1024;
    return SDR_OK;
}

int SDF_GenerateRandom(void *hSessionHandle, unsigned int uiLength, unsigned char *pucRandom) {
    // 模拟产生随机数
    for (unsigned int i = 0; i < uiLength; i++) {
        pucRandom[i] = rand() % 256;
    }
    return SDR_OK;
}

int SDF_Encrypt(void *hSessionHandle, void *hKeyHandle, unsigned int uiAlgID,
                unsigned char *pucIV, unsigned char *pucData, unsigned int uiDataLength,
                unsigned char *pucEncData, unsigned int *puiEncDataLength) {
    // 模拟对称加密
    memcpy(pucEncData, pucData, uiDataLength);
    *puiEncDataLength = uiDataLength;
    return SDR_OK;
}

int SDF_Decrypt(void *hSessionHandle, void *hKeyHandle, unsigned int uiAlgID,
                unsigned char *pucIV, unsigned char *pucEncData, unsigned int uiEncDataLength,
                unsigned char *pucData, unsigned int *puiDataLength) {
    // 模拟对称解密
    memcpy(pucData, pucEncData, uiEncDataLength);
    *puiDataLength = uiEncDataLength;
    return SDR_OK;
}
